/**
 * Category DAO
 */
var DB_COLLECTION_CATEGORY_TB_NAME = "category";
var DB_COLLECTION_CATEGORY_COL_ID = '_id';
var DB_COLLECTION_CATEGORY_COL_TITLE = "title";
var DB_COLLECTION_CATEGORY_COL_PID = "pid";
var DB_COLLECTION_CATEGORY_COL_PATH = "path";
// create date
// modify date

var db = require("./dbMongo");
var BSON = require('mongodb').BSONPure;


exports.DB_COLLECTION_CATEGORY_COL_ID = DB_COLLECTION_CATEGORY_COL_ID;
exports.DB_COLLECTION_CATEGORY_COL_TITLE = DB_COLLECTION_CATEGORY_COL_TITLE;
exports.DB_COLLECTION_CATEGORY_COL_PID = DB_COLLECTION_CATEGORY_COL_PID;
exports.DB_COLLECTION_CATEGORY_COL_PATH = DB_COLLECTION_CATEGORY_COL_PATH;

exports.all = function(handler) {
	db.connect();
	db.find(DB_COLLECTION_CATEGORY_TB_NAME, handler);
};

exports.findById = function(id, handler){
	db.connect();
	var cid = BSON.ObjectID.createFromHexString(id);
	var key = {_id : cid};
	db.listQuery(DB_COLLECTION_CATEGORY_TB_NAME, key, function(err, items){
		if (err) {
			handler(err, null);
		} else {
			console.log('[categoryDao.findById], key is ' + key + ', result is ' + items);
			handler(null, items[0]);
		}
	});
}

exports.add = function(item, handler) {
	db.connect();
	db.add(DB_COLLECTION_CATEGORY_TB_NAME, item, handler);
};